/*
 *  listener.cc
 *  Untitled
 *
 *  Created by Ben Nolan on 3/11/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "listener.h"
#include "server_region.h"
#include "message.h"
#include "node.h"

namespace Wonderland{

	Listener::Listener(boost::asio::io_service& io, ServerRegion *region) : XmlConnection(io),  region_(region){
		interestBox_ = AxisAlignedBox(Vector(-1000,-1000,-1000), Vector(1000,1000,1000));
	}

	std::vector<Node *> Listener::getVisibleNodes(){
		return region_->findNodesWithin(interestBox_);
	}

	bool Listener::interestedIn(const Node node){
		return interestBox_.contains(node.getPosition());
	}

	void Listener::onMessage(Message& m){
		if(m.isXml()){
			
			if(m.rootElement()->Value() == (std::string) "node"){
				region_->addNode(new Node(m.rootElement()));
			}
		
		}
		
		std::cout << "Message recieved...\n";
	}
	
	void Listener::onConnected(){
		std::cout << "Listener connected...\n";
		region_->addListener(this);
	}
	
	void Listener::onDisconnected(){
		std::cout << "Listener disconnected.\n";
		region_->removeListener(this);
	}
	
}
